﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebsiteBanThucAn.Models;
using System.Data;
using WebsiteBanThucAn.DAL;

namespace DAL
{
    public class DatHangDAL
    {
        private static void GanDoiTuong(DatHangModel dhm, DataRow dr)
        {// Gán từng thuộc tính cho đối tượng
            dhm.DathangID = int.Parse(dr[DatHangModel.dathangid].ToString());
            dhm.KhachhangID = int.Parse(dr[DatHangModel.khachhangid].ToString());
            dhm.Ngaydathang = DateTime.Parse(dr[DatHangModel.ngaydathang].ToString());
            dhm.Trigia = float.Parse(dr[DatHangModel.trigia].ToString());
            dhm.Dagiao = bool.Parse(dr[DatHangModel.dagiao].ToString());
            dhm.Ngaygiao = DateTime.Parse(dr[DatHangModel.ngaygiao].ToString());
        }

        public static List<DatHangModel> LayDSDatHangTatCa()
        {// Lấy danh sách tất cả đặt hàng
            List<DatHangModel> list = new List<DatHangModel>();
            try
            {
                string sql = string.Format("select * from {0}", DatHangModel.dathang);
                DataTable dt = SqlDataProvider.DocBang(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    DatHangModel dhm = new DatHangModel();
                    GanDoiTuong(dhm, dr);
                    list.Add(dhm);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return list;
        }

        //public static DatHangModel LayThongTinDatHangTheoKhachHangID(int id)
        //{

        //}

        public static bool ThemDatHang(DatHangModel dhm)
        {
            bool result = false;
            try
            {
                string sql = string.Format("insert into {0} values ", DatHangModel.dathang);
                sql += string.Format("('{0}','{1}','{2}','{3}','{4}')",
                    dhm.KhachhangID, dhm.Ngaydathang, dhm.Trigia, dhm.Dagiao, dhm.Ngaygiao);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        public static bool XoaDatHang(int id)
        {
            bool result = false;
            try
            {
                string sql = string.Format("delete {0} where {1}='{2}'", DatHangModel.dathang, DatHangModel.dathangid, id);
                int n = SqlDataProvider.ThucHienLenh(sql);
                if (n == 1)
                {
                    result = true;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return result;
        }

        //public static bool SuaDatHang(DatHangModel dhm)
        //{

        //}
    }
}
